package com.infonam.media.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;

import com.infonam.media.database.InfoNamMediaContract.UserTable;
import com.infonam.media.database.entity.User;

public class DatabaseHelper extends SQLiteOpenHelper {

	public static final int DATABASE_VERSION = 1;
	public static final String DATABASE_NAME = "InfoNamMedia.db";

	private static final String TEXT_TYPE = " TEXT";
	private static final String COMMA_SEP = ",";
	private static final String SQL_CREATE_UserTable = "CREATE TABLE "
			+ UserTable.TABLE_NAME + " (" + UserTable._ID
			+ " INTEGER PRIMARY KEY," + UserTable.COLUMN_NAME_User_Id
			+ TEXT_TYPE + COMMA_SEP + UserTable.COLUMN_NAME_Pass + TEXT_TYPE
			+ " )";

	private static final String SQL_DELETE_UserTable = "DROP TABLE IF EXISTS "
			+ UserTable.TABLE_NAME;

	public DatabaseHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	public void onCreate(SQLiteDatabase db) {
		db.execSQL(SQL_CREATE_UserTable);
	}

	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL(SQL_DELETE_UserTable);
		onCreate(db);
	}

	public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		onUpgrade(db, oldVersion, newVersion);
	}

	// Adding new user
	public long addUser(User user) {
		SQLiteDatabase db = this.getWritableDatabase();

		ContentValues values = new ContentValues();
		values.put(UserTable.COLUMN_NAME_User_Id, user.getUserID()); // Contact
																		// Name
		values.put(UserTable.COLUMN_NAME_Pass, user.getPassword()); // Contact
																	// Phone

		// Inserting Row
		long rowid = 0;
		try {
			rowid = db.insert(UserTable.TABLE_NAME, null, values);
		} catch (SQLiteException e) {

		} finally {
			db.close();
		}
		// Closing database connection

		return rowid;
	}

	// Getting single user
	public User getUser(String userid) {
		SQLiteDatabase db = this.getReadableDatabase();
		String[] projection = { UserTable._ID, UserTable.COLUMN_NAME_User_Id,
				UserTable.COLUMN_NAME_Pass };

		Cursor cursor = db.query(UserTable.TABLE_NAME, projection,
				UserTable.COLUMN_NAME_User_Id + "=?", new String[] { userid },
				null, null, null, null);
		try {
			if (cursor != null) {
				cursor.moveToFirst();
				if (cursor.getCount() != 0) {
					User user = new User(cursor.getString(1),
							cursor.getString(2));
					return user;
				} else {

					return null;
				}
			}
		} catch (SQLiteException e) {

		} finally {
			cursor.close();
			db.close();
		}

		return null;
	}

	public boolean checkUser(String userID, String pass) {
		SQLiteDatabase db = this.getReadableDatabase();
		String[] projection = { UserTable._ID, UserTable.COLUMN_NAME_User_Id,
				UserTable.COLUMN_NAME_Pass };

		Cursor cursor = db.query(UserTable.TABLE_NAME, projection,
				UserTable.COLUMN_NAME_User_Id + "=? and "
						+ UserTable.COLUMN_NAME_Pass + "=?", new String[] {
						userID, pass }, null, null, null, null);
		try {
			if (cursor != null) {
				if (cursor.getCount() != 0) {

					return true;
				} else {
					return false;
				}
			}
		} catch (SQLiteException e) {

		} finally {
			cursor.close();
			db.close();
		}
		return false;
	}

	public static boolean checkDataBase() {
		SQLiteDatabase checkDB = null;
		try {
			checkDB = SQLiteDatabase.openDatabase(DATABASE_NAME, null,
					SQLiteDatabase.OPEN_READONLY);
			checkDB.close();
		} catch (SQLiteException e) {
			// database doesn't exist yet.
		}
		return checkDB != null ? true : false;
	}

}
